/*
 * Copyright 2010 Pierre Heinzerling, Uwe Janner, Patrick Wright
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.upp.publinksky.model;

import com.google.appengine.api.datastore.Key;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Date;

/**
 */
@PersistenceCapable
@XmlRootElement(name = "theme")
public class Theme {
    static final public String SHARE_NAME_SCRATCHPAD = "ScratchPad";

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    @Persistent
    private String ownerEmail;
    @Persistent
    private String name;
    @Persistent
    private Date createdOn;
    @Persistent
    private int maxScore;
    @Persistent
    private int lastUpdateCounter;
    @Persistent
    private int ownerLastRetrievedCounter;
    @Persistent
    private boolean lastUpdatedByOwner;
    @Persistent
    private String lastUpdateByEmail;

    public Theme() {
    }

    public static Theme newScratchPad(final String ownerEmail) {
        Theme theme = new Theme();
        theme.setOwnerEmail(ownerEmail);
        theme.setName(SHARE_NAME_SCRATCHPAD);
        theme.setCreatedOn(new Date());
        theme.setLastUpdateCounter(0);
        theme.setMaxScore(1);
        theme.setOwnerLastRetrievedCounter(0);
        return theme;
    }

    public String getOwnerEmail() {
        return ownerEmail;
    }

    public void setOwnerEmail(final String ownerEmail) {
        this.ownerEmail = ownerEmail;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public Date getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(final Date createdOn) {
        this.createdOn = createdOn;
    }

    public String toString() {
        return "Theme(by: " + ownerEmail + ", name " + name + ", on " + createdOn + ")";
    }

    public Key getKey() {
        return key;
    }

    public void setKey(final Key key) {
        this.key = key;
    }

    public int getMaxScore() {
        return maxScore;
    }

    public void setMaxScore(final int maxScore) {
        this.maxScore = maxScore;
    }

    public int getLastUpdateCounter() {
        return lastUpdateCounter;
    }

    public void setLastUpdateCounter(final int lastUpdateCounter) {
        this.lastUpdateCounter = lastUpdateCounter;
    }

    public int getOwnerLastRetrievedCounter() {
        return ownerLastRetrievedCounter;
    }

    public void setOwnerLastRetrievedCounter(final int ownerLastRetrievedCounter) {
        this.ownerLastRetrievedCounter = ownerLastRetrievedCounter;
    }

    public boolean hasOwnerRetrievedSinceLastModified() {
        return this.ownerLastRetrievedCounter == this.lastUpdateCounter;
    }

    public boolean isOwner(final String email) {
        return getOwnerEmail().equals(email);
    }

    public void markUpdated(final String byEmail) {
        setLastUpdateCounter(getLastUpdateCounter() + 1);
        setLastUpdatedByOwner(isOwner(byEmail));
        setLastUpdateByEmail(byEmail);
    }

    public boolean isScratchPad() {
        return name != null && name.equals(SHARE_NAME_SCRATCHPAD);
    }

    public void setLastUpdatedByOwner(final boolean lastUpdatedByOwner) {
        this.lastUpdatedByOwner = lastUpdatedByOwner;
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (!(o instanceof Theme)) return false;

        final Theme theme = (Theme) o;

        if (createdOn != null ? !createdOn.equals(theme.createdOn) : theme.createdOn != null) return false;
        if (key != null ? !key.equals(theme.key) : theme.key != null) return false;
        if (name != null ? !name.equals(theme.name) : theme.name != null) return false;
        if (ownerEmail != null ? !ownerEmail.equals(theme.ownerEmail) : theme.ownerEmail != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = key != null ? key.hashCode() : 0;
        result = 31 * result + (ownerEmail != null ? ownerEmail.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (createdOn != null ? createdOn.hashCode() : 0);
        return result;
    }

    public boolean isLastUpdatedByOwner() {
        return lastUpdatedByOwner;
    }

    public String getLastUpdateByEmail() {
        return lastUpdateByEmail;
    }

    public void setLastUpdateByEmail(final String lastUpdateByEmail) {
        this.lastUpdateByEmail = lastUpdateByEmail;
    }

    public boolean wasLastUpdatedBy(final String email) {
        return lastUpdateByEmail != null && lastUpdateByEmail.equals(email);
    }
}
